package design;

import java.util.ArrayList;
import java.util.List;

public class Client {
    //抽象观察者(被通知)
    public interface IObserver<E>{
        void update(E event);
    }

    //抽象主题者(通知方)
    public interface ISubject<E>{
        boolean attach(IObserver<E> observer);
        boolean detach(IObserver<E> observer);
        void notify(E event);
    }

    static class ConcreteObserver<E> implements IObserver<E>{
        public void update(E event) {
            System.out.println("receive event "+event);
        }
    }

    //具体主题者
    static class ConcreteSubject<E> implements ISubject<E>{

        private List<IObserver<E>> observers = new ArrayList<IObserver<E>>();

        public boolean attach(IObserver<E> observer) {
            return !this.observers.contains(observer) && this.observers.add(observer);
        }

        public boolean detach(IObserver<E> observer) {
            return this.observers.remove(observer);
        }

        public void notify(E event) {
            for (IObserver<E> observer : this.observers) {
                observer.update(event);
            }
        }
    }

    public static void main(String[] args) {
        //通知方
        ISubject<String> observable = new ConcreteSubject<String>();
        //被通知方
        IObserver<String> observer = new ConcreteObserver<String>();
        //注册
        observable.attach(observer);
        //通知
        observable.notify("hello");
    }

}
